﻿using JT808.Protocol.Extensions;
using JT808.Protocol.MessageBody;
using JT808.Protocol.Interfaces;
using System;
using JT808.Protocol.MessagePack;
using System.Collections.Generic;

namespace JT808.Protocol.Formatters.MessageBodyFormatters
{
    public class JT808_0x8900_Formatter : IJT808MessagePackFormatter<JT808_0x8900>
    {
        public JT808_0x8900 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            JT808_0x8900 jT808_0X8900 = new JT808_0x8900();
            jT808_0X8900.passthroughID = reader.ReadByte();
            jT808_0X8900.operationType = reader.ReadByte();
            jT808_0X8900.zno = reader.ReadByte();
            var list = new List<JT808_0x8900_Sub>();
            for (int i = 0; i < jT808_0X8900.zno; i++)
            {
                list.Add(new JT808_0x8900_Sub
                {
                    pressHigh = reader.ReadByte(),
                    pressLow = reader.ReadByte()
                });
            }
            jT808_0X8900.temperatureHigh = reader.ReadByte();
            return jT808_0X8900;
        }

        public void Serialize(ref JT808MessagePackWriter writer, JT808_0x8900 value, IJT808Config config)
        {
            writer.WriteByte(value.passthroughID);
            writer.WriteByte(value.operationType);
            writer.WriteByte(value.zno);
            if (value.sub != null)
            {
                foreach (var item in value.sub)
                {
                    writer.WriteByte(item.pressHigh);
                    writer.WriteByte(item.pressLow);
                }
            }
            writer.WriteByte(value.temperatureHigh);
        }
    }
}
